package com.aaa.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.entity.log.Log;
import com.aaa.entity.system.User;
import com.aaa.service.log.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;
//    org.springframework.aop.aspectj.AspectJPointcutAdvisor;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/9/8 14:15
 * @description：日志
 * @modified By：
 * @version: 1.0
 */
@Component
@Aspect
@EnableAspectJAutoProxy
public class LogAspect {
    @Autowired
    LogService logService;
/*
    @Pointcut("execution(public * com.aaa.service.*.*.*.*(..))&&!execution(public * com.aaa.service.log.impl.LogServiceImpl.*(..))")
    public void aaa(){
    }
    @Around("aaa()")
    public Object myEnhance( ProceedingJoinPoint joinPoint) throws UnknownHostException {
        //获取方法的参数
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        //保存日志信息到数据库中
        Log log = new Log();
        String loginName = log.getLoginName();
        //获取请求的ip地址
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        InetAddress localHost = InetAddress.getLocalHost();
        String hostAddress = localHost.getHostAddress();
        log.setIp(hostAddress);
        HttpSession session = request.getSession();
        //获取类名
        String className = joinPoint.getTarget().getClass().getName();
        log.setClassName(className);
        //获取方法名
        log.setMethodName(signature.getName());
        //获取参数
        String paramString = Arrays.toString(args);
        log.setParams(paramString);
        if (paramString.getBytes().length > 8889) {
            log.setParams("Too Long Params To Save ");
        }
        //设置时间
        log.setCreateTime(new Date());
        if (ObjectUtil.isNotEmpty(session.getAttribute("user"))) {
            User user = (User) session.getAttribute("user");
            log.setUserName(user.getUsername());
            log.setLoginName(user.getUsername());
            //保存日志到数据库中
            logService.insertLog(log);
        }
        try {
            //业务方法执行
            Object proceed = joinPoint.proceed(args);
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }*/
}